import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'

export const routes = [
  {
    path: '/',
    component: () => import(/*webpackChunkName:"layout" */'@/views/layout.vue'),
    redirect: '/introduce',
    children: [
      {
        path: '/introduce',
        meta: {
          groupTitle: "Dashboard",
          title: "系统介绍",
          icon: 'el-icon-eleme',
          role: ['master', 'admin', 'user']
        },
        component: () => import(/*webpackChunkName:"introduce" */'@/views/introduce.vue')
      },
      {
        path: '/add',
        meta: {
          groupTitle: "Dashboard",
          title: "添加商品",
          icon: 'el-icon-plus',
          role: ['master']
        },
        component: () => import(/*webpackChunkName:"add" */'@/views/add.vue')
      },
      {
        path: '/swiper',
        meta: {
          groupTitle: "首页配置",
          title: "轮播设置",
          icon: 'el-icon-camera',
          role: ['master', 'admin']
        },
        component: () => import(/*webpackChunkName:"add" */'@/views/add.vue')
      },
      {
        path: '/hot',
        meta: {
          groupTitle: "首页配置",
          title: "热销商品",
          icon: 'el-icon-s-promotion',
          role: ['master']
        },
        component: () => import(/*webpackChunkName:"add" */'@/views/add.vue')
      },
      {
        path: '/goods',
        meta: {
          groupTitle: "模块管理",
          title: "商品管理",
          icon: 'el-icon-s-promotion',
          role: ['master']
        },
        component: () => import(/*webpackChunkName:"add" */'@/components/HelloWorld.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
let isLogin = true;
let freeRoutes = ['/login']
router.beforeEach((to, from, next) => {
  if (freeRoutes.indexOf(to.path) > -1) {
    return next()
  }
  if (isLogin) {
    next()
  } else {
    next('/login')
  }
})

export default router
